<?php
// 本类由系统自动生成，仅供测试用途
class RbacAction extends CommonAction {

	/**进入权限管理首页**/
	public function index(){

		$this->display();
	}

	/**节点树列表***/
	public  function noteList(){
		$this->display();
	}
	
	/**用户列表***/
	public  function userList(){		
		import("ORG.Util.AjaxPage");// 导入分页类  注意导入的是自己写的AjaxPage类
		$credit = M('profile');
		$count = $credit->count(); //计算记录数
		$limitRows = 10; // 设置每页记录数
		$p = new AjaxPage($count, $limitRows,"queryPage"); //第三个参数是你需要调用换页的ajax函数名
		$limit_value = $p->firstRow . "," . $p->listRows;
		$data = $credit->order('id desc')->limit($limit_value)->select(); // 查询数据
		$page = $p->show(); // 产生分页信息，AJAX的连接在此处生成
		$this->assign('list',$data);
		$this->assign('page',$page);
		$this->display();
	}
	
	
	/**查询角色列表***/
	public function roleList(){
		
		import("ORG.Util.AjaxPage");// 导入分页类  注意导入的是自己写的AjaxPage类
		$credit = M('role');
		$count = $credit->count(); //计算记录数
		$limitRows = 10; // 设置每页记录数
		$p = new AjaxPage($count, $limitRows,"queryPage"); //第三个参数是你需要调用换页的ajax函数名
		$limit_value = $p->firstRow . "," . $p->listRows;
		$data = $credit->order('id desc')->limit($limit_value)->select(); // 查询数据
		$page = $p->show(); // 产生分页信息，AJAX的连接在此处生成
		$this->assign('list',$data);
		$this->assign('page',$page);
		$this->display();
	}
	
	/**获取角色***/
	public function  getRoleJson(){
		//$db=M("role");
		//$nodes=$db->select();
		//$nodes2=$db->where(" in (select role_id from wx_role_user where user_id=)"+M("userId"))->order("sort asc ")->select();
		
		$uid=I("uid",0);
		$ns=new Model();//等价与$user=M();		
		//$list=$ns->query('select id,name,pid,IF(role_id is not null,"true","false") as checked from wx_role r left join wx_role_user ru on r.id=ru.role_id  where  user_id='.$uid.' or user_id is null');
		
		$c = array();
		$list=$ns->query('select id,name,pid,"false" as checked from wx_role');
		$list2=$ns->query('select role_id from wx_role_user  where  user_id='.$uid);
		foreach ($list as $o){
			foreach ($list2 as $rid){
				if($o['id']==$rid['role_id']){
					
					$o['checked']='true';
					break;
				}
			}
			$c[]=$o;
		}
		outJson($c);
	}
	
	
	/**节点树形结构***/
	public function getNotesJson(){
		$db=M("node");
		$nodes=$db->order("sort asc ")->select();
		outJson($nodes);
	}
	
	/**获取节点***/
	public function  getNodeJson(){
		$uid=I("nid",0);
		$ns=new Model();
		$c = array();
		$list=$ns->query('select id,name,pid as pId,"false" as checked from wx_node');
		$list2=$ns->query('select node_id from wx_access  where  role_id='.$uid);
		foreach ($list as $o){
			foreach ($list2 as $rid){
				if($o['id']==$rid['node_id']){
					$o['checked']='true';
					break;
				}
			}
			$c[]=$o;
		}
		outJson($c);
	}
	
	/**保存角色节点关系***/
	public function  saveRole(){
		$nids=I("nids",'');
		$roleid=I("roleid",'');
		$arraylist=split(",",$nids);
		$ur=M("access");
		$ur->where('role_id='.$roleid)->delete(); // 删除
		for($i=0;$i<count($arraylist);$i++) //把它们全部输出来
		{
			$level = M("node")->where('id='.$arraylist[$i])->getField('level');
			$data["node_id"]=$arraylist[$i];
			$data["role_id"]=$roleid;
			$data["level"]=$level;
			$ur->add($data);
		}
		outSuccess("ok");
	}
	public function saveUserRole(){
		$rids=I("rids",'');
		$userid=I("userid",'');
		$arraylist=split(",",$rids);
		$ur=M("role_user");
		$ur->where('user_id='.$userid)->delete(); // 删除
		for($i=0;$i<count($arraylist);$i++) //把它们全部输出来
		{
			$data["role_id"]=$arraylist[$i];
			$data["user_id"]=$userid;
			$ur->add($data);
		}
		outSuccess("ok");
	}
}